<template>
  <div class="add-box">
    <el-form :model="form" label-position="top">
      <el-form-item label="账户类型">
        <el-select v-model="form.accountType" placeholder="请选择">
          <el-option v-for="item in typeOptions" :key="item.id" :label="item.name" :value="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="账户名称">
        <el-input v-model="form.accountName"></el-input>
      </el-form-item>
      <el-form-item label="初始金额">
        <el-input v-model.number="form.initialMoney"></el-input>
      </el-form-item>
      <el-form-item>
        <el-checkbox v-model="form.state" :true-label="0" :false-label="1">启用</el-checkbox>
      </el-form-item>
      <el-form-item label="备注">
        <el-input type="textarea" :rows="2" v-model="form.remark"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="$emit('changeState',false)">取 消</el-button>
      <el-button type="primary" @click="submit">保存</el-button>
    </div>
  </div>
</template>
<script>
import { save } from '@/api/account'
export default {
  data() {
    return {
      form: {
        companyId: this.$store.state.user.companyId,
        accountName: '',
        accountType: '',
        initialMoney: 0,
        state: 0,
        remark: ''
      },
      typeOptions: [
        {
          id: '0',
          name: '现金'
        },
        {
          id: '1',
          name: '储蓄卡'
        },
        {
          id: '2',
          name: '信用卡'
        },
        {
          id: '3',
          name: '支付宝'
        },
        {
          id: '4',
          name: '微信'
        },
        {
          id: '5',
          name: '其它资金'
        }
      ]
    }
  },
  beforeDestroy() {
    this.$off('reload')
    this.$off('changeState')
  },
  methods: {
    submit() {
      save(this.form).then(res => {
        if (res.success) {
          this.$emit('reload')
          this.$emit('changeState', false)
          this.$message.success(res.message)
        } else {
          this.$message.error(res.message)
        }
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.add-box {
  padding: 0 50px;
  overflow-y: auto;
}
.dialog-footer {
  text-align: right;
}
</style>
